<?php
class MonsterService extends AbstractService {
    /**
     * @var MonsterService
     */
    private static $instance;

    /**
     * Get the instance of MonsterService.
     *
     * @return MonsterService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new MonsterService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    protected function __construct()
    {
        parent::__construct();
    }

    /**
     * 处理用户创建时的逻辑
     *
     * @param int $shardId
     * @return void
     */
    public function handleCreate($shardId)
    {

    }

    /**
     * 处理用户登录时的逻辑
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId)
    {
        $this->initData($userId);
    }

    /**
     * 处理 service 对外提供的全量数据
     *
     * @param int $userId
     * @return array
     */
    public function info($userId)
    {

    }

    /**
     * @param $userId
     * @param $monsterId
     * @param $reqTime
     * @return int
     * @throws SzException
     */
    public function award($userId,$monsterId,$reqTime) {
        $monsterSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GROUND_MONSTER, $monsterId, true);
        if (!$monsterSetting) {
            throw new SzException(29015,$monsterId);
        }

        $num = $this->initData($userId,$reqTime);
        $awardLimit = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'monsterReward', true);
        if ($num >= $awardLimit['value']) {
            throw new SzException(29016,$num,$awardLimit);
        }

        list($min,$max) = $monsterSetting['award_num'];
        $award_num = mt_rand($min,$max);

        GameService::get()->handleEntity($userId, $monsterSetting['award_item'], $award_num, true);
        CommonFunctionService::get()->reportResourceUseDetails(99999994,$userId,1,$monsterSetting['award_item'].':'.$award_num.'|','award');//获得资源上报

        /**
         * @var ModuleProfileInfoVo $profileInfo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_DAILY_GROUND_MONSTER, false);
        $num += 1;
        $profileInfo->setInfo(intval($num));
        $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_DAILY_GROUND_MONSTER, $profileInfo);
        SzPersister::get()->setVoList($profileInfoList);

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:map|action:beat|',
                'item' => $monsterId,
                'value' => 1
            )
        ));

        return array(
            $monsterSetting['award_item']=>$award_num
        );
    }

    /**
     * @param $userId
     * @param null $reqTime
     * @return int|string
     * @throws SzException
     */
    public function initData($userId, $reqTime = null) {
        if (!$reqTime) {
            $reqTime = GameService::get()->getReqTime();
        }
        /**
         * @var ModuleProfileInfoVo $profileInfo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_DAILY_GROUND_MONSTER, false);
        if (!$profileInfo) {
            $profileInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::USER_INFO_TYPE_DAILY_GROUND_MONSTER,
                0,
                $reqTime,
                true
            );
            $profileInfoList->addElement($profileInfo);
            $profileInfoList->setElement(GameConst::USER_INFO_TYPE_DAILY_GROUND_MONSTER, $profileInfo);
            SzPersister::get()->setVoList($profileInfoList);
            $num = 0;
        } else {
            // 日期不一致
            if(SzTime::getDate($profileInfo->getTime()) != SzTime::getDate($reqTime)) {
                $profileInfo->setTime($reqTime);
                $profileInfo->setInfo(0);
                $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_DAILY_GROUND_MONSTER, $profileInfo);
                SzPersister::get()->setVoList($profileInfoList);
            }
            $num = $profileInfo->getInfo();
        }
        return $num;
    }

    /**
     * @param $userId
     * @param $reqTime
     * @return mixed
     * @throws SzException
     */
    public function refreshTime($userId,$reqTime)
    {
        /**
         * @var ModuleProfileInfoVo $profileInfo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_DAILY_GROUND_MONSTER_REFRESH, false);
        if (!$profileInfo) {
            $profileInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::USER_INFO_TYPE_DAILY_GROUND_MONSTER_REFRESH,
                intval($reqTime),
                $reqTime,
                true
            );
            $profileInfoList->addElement($profileInfo);
            $profileInfoList->setElement(GameConst::USER_INFO_TYPE_DAILY_GROUND_MONSTER_REFRESH, $profileInfo);
            SzPersister::get()->setVoList($profileInfoList);
        } else {
            $profileInfo->setTime($reqTime);
            $profileInfo->setInfo(intval($reqTime));
            $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_DAILY_GROUND_MONSTER_REFRESH, $profileInfo);
            SzPersister::get()->setVoList($profileInfoList);
        }
        return $reqTime;
    }
}